package edu.the.last.chord.factory.simple;

import java.util.ArrayList;
import java.util.List;

import edu.the.last.Messages;
import edu.the.last.chord.factory.ChordInterface;

/**
 * suggest chords using a fairly simple algorithm
 * 
 * @author albertcsani
 * 
 */

public class SimpleChord implements ChordInterface {
	private String chords[] = null;
	private SimpleScale scales[] = new SimpleScale[5];
	private String[] musicSounds = { Messages.getString("SimpleChord.0"), Messages.getString("SimpleChord.1"), Messages.getString("SimpleChord.2"), Messages.getString("SimpleChord.3"), Messages.getString("SimpleChord.4"), Messages.getString("SimpleChord.5"), Messages.getString("SimpleChord.6"), Messages.getString("SimpleChord.7"), Messages.getString("SimpleChord.8"), Messages.getString("SimpleChord.9"), Messages.getString("SimpleChord.10"), Messages.getString("SimpleChord.11") }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$

	/**
	 * constructs the recognized scales
	 */
	public SimpleChord() {
		// A dur
		List<String> noteA = new ArrayList<String>();
		noteA.add(Messages.getString("SimpleChord.12")); //$NON-NLS-1$
		noteA.add(Messages.getString("SimpleChord.13")); //$NON-NLS-1$
		noteA.add(Messages.getString("SimpleChord.14")); //$NON-NLS-1$
		noteA.add(Messages.getString("SimpleChord.15")); //$NON-NLS-1$
		noteA.add(Messages.getString("SimpleChord.16")); //$NON-NLS-1$
		scales[0] = new SimpleScale(Messages.getString("SimpleChord.17"), noteA); //$NON-NLS-1$
		// E dur
		List<String> noteE = new ArrayList<String>();
		noteE.add(Messages.getString("SimpleChord.18")); //$NON-NLS-1$
		noteE.add(Messages.getString("SimpleChord.19")); //$NON-NLS-1$
		noteE.add(Messages.getString("SimpleChord.20")); //$NON-NLS-1$
		noteE.add(Messages.getString("SimpleChord.21")); //$NON-NLS-1$
		noteE.add(Messages.getString("SimpleChord.22")); //$NON-NLS-1$
		scales[1] = new SimpleScale(Messages.getString("SimpleChord.23"), noteE); //$NON-NLS-1$
		// f#m
		List<String> noteFism = new ArrayList<String>();
		noteFism.add(Messages.getString("SimpleChord.24")); //$NON-NLS-1$
		noteFism.add(Messages.getString("SimpleChord.25")); //$NON-NLS-1$
		noteFism.add(Messages.getString("SimpleChord.26")); //$NON-NLS-1$
		noteFism.add(Messages.getString("SimpleChord.27")); //$NON-NLS-1$
		noteFism.add(Messages.getString("SimpleChord.28")); //$NON-NLS-1$
		scales[2] = new SimpleScale(Messages.getString("SimpleChord.29"), noteFism); //$NON-NLS-1$
		// D dur
		List<String> noteD = new ArrayList<String>();
		noteD.add(Messages.getString("SimpleChord.30")); //$NON-NLS-1$
		noteD.add(Messages.getString("SimpleChord.31")); //$NON-NLS-1$
		noteD.add(Messages.getString("SimpleChord.32")); //$NON-NLS-1$
		noteD.add(Messages.getString("SimpleChord.33")); //$NON-NLS-1$
		noteD.add(Messages.getString("SimpleChord.34")); //$NON-NLS-1$
		scales[3] = new SimpleScale(Messages.getString("SimpleChord.35"), noteD); //$NON-NLS-1$
		// C dur
		List<String> noteC = new ArrayList<String>();
		noteC.add(Messages.getString("SimpleChord.36")); //$NON-NLS-1$
		noteC.add(Messages.getString("SimpleChord.37")); //$NON-NLS-1$
		noteC.add(Messages.getString("SimpleChord.38")); //$NON-NLS-1$
		noteC.add(Messages.getString("SimpleChord.39")); //$NON-NLS-1$
		noteC.add(Messages.getString("SimpleChord.40")); //$NON-NLS-1$
		scales[4] = new SimpleScale(Messages.getString("SimpleChord.41"), noteC); //$NON-NLS-1$
	}

	private String getChord(int[] sounds) {
		int grades[] = new int[scales.length];
		for (int i = 0; i < sounds.length; i++) {
			for (int j = 0; j < scales.length; j++) {
				if (sounds[i]!=-1) {
					grades[j] += scales[j].getNoteValue(musicSounds[sounds[i]]);
				} else {
					grades[j] += scales[j].getNoteValue(Messages.getString("SimpleChord.Nothin")); //$NON-NLS-1$
				}
			}
		}
		int ind = 0;
		for (int i = 1; i < grades.length; i++) {
			if (grades[i] > grades[ind]) {
				ind = i;
			}
		}
		return scales[ind].getScale();
	}

	/**
	 * will generate chords according to the sounds in a bar
	 * 
	 * @param sounds
	 *            the sounds
	 * @param bpb
	 *            beat per bar
	 * @return array of strings
	 */
	@Override
	public String[] getChords(int[] sounds, int bpb, int bpm) {
		int noOfChords = sounds.length / 2 / bpb + 1;
		chords = new String[noOfChords];
		for (int i = 0; i < noOfChords; i++) {
			int temp[] = new int[bpb * 2];
			for (int j = 0; j < bpb * 2 && (i * bpb * 2 + j) < sounds.length; j++) {
				temp[j] = sounds[i * bpb * 2 + j];
			}
			chords[i] = getChord(temp);
		}
		return chords;
	}

	@Override
	public String toString() {
		return Messages.getString("SimpleChord.Name"); //$NON-NLS-1$
	}
}
